$(function () {
    /*
     @
     */

    $("#singUpBtn").bind("click", function () {
        if ($("#sing").valid()) {
            if ($(this).hasClass("dis")) {
                $(".sTip").show().animate({"bottom": "10px", "left": "250px"});
                return false;
            }
            $("#sing").submit();
        }
    })
    $("#sing").delegate("input", "focus", function () {
        showSingTip(this);
    });
    $("#sing").delegate("input", "blur", function () {
        inputBlur(this);
        /*switch(sValue){
         case:
         }*/
    });
    $(".addMember").click(function () {
        var warp = $(".memberWrap"), warpId = warp.length + 2;
        $("#quantity").val(warpId);
        var Obj = warp.last().clone().attr("id", warpId), newInputs = Obj.find("input");
        Obj.find(".singTip").hide();
        Obj.find(".errorTip").hide();
        Obj.find(".passTip").hide();
        !(Obj.find(".spliceLine").length) && Obj.prepend('<span class="spliceLine"></span>');
        $.each(newInputs, function (item) {
            var _thisId = $(this).attr("data-id");
            $(this).attr("name", _thisId + warpId).attr("id", _thisId + warpId);
        });
        Obj.find("input").val("");
        Obj.insertBefore(".addMember");
        if ($(".memberWrap").length == 7) {
            $(this).hide();
            return false;
        }
    })
    /*
     * 拓展validate方法 isNickname()
     * @param value:表单的值
     * @param element:表单
     * @rule param[/^[\w\u4E00-\u9FA5\uF900-\uFA2D]{2,24}$/]  nickname 只能输入英文、中文、数字、下划线
     * 务必保持param=/^[\w\u4E00-\u9FA5\uF900-\uFA2D]{2,24}$/
     * */
    $.validator.addMethod("isNickname", function (value, element, params) {
        params = /^[\w\u4E00-\u9FA5\uF900-\uFA2D]{2,24}$/;
        if (value == "") {
            return true;
        }
        return (params.test(value));
    });
    /*
     * 拓展validate方法 notEqualTo()
     * @param value:表单的值
     * @param element:表单
     * @rule 表单内容不能相同
     * 务必保持为数组，值为表单ID"#inputID"形式
     * */
    $.validator.addMethod("notEqualTo", function (value, element, params) {
        if (value == "") {
            return true;
        }
        var returnVal = true;
        $.each(params, function (i, item) {
            var _value = value, _ele = element;
            returnVal = !(value && (value === $(item).val()));
            return returnVal;
        })
        return returnVal;
    });
    $("#sing").validate({
        errorLabelContainer: ".validateTip",
        wrapper: "label",
        rules: {
            family: {required: true, isNickname: ''},
            email1: {required: true,email: true, notEqualTo: [ "#email2", "#email3", "#email4", "#email5", "#email6", "#email7"]},
            password1: {required: true},
            nickname1: {required: true, isNickname: '',notEqualTo: ["#nickname2", "#nickname3", "#nickname4", "#nickname5", "#nickname6", "#nickname7"]},
            email2: {required: function () {
                return ($("#password2").val().length > 0 || $("#nickname2").val().length > 0 ? true : false)
            }, email: true, notEqualTo: ["#email1", "#email3", "#email4", "#email5", "#email6", "#email7"]},
            password2: {required: function () {
                return ($("#email2").val().length > 0 || $("#nickname2").val().length > 0 ? true : false)
            }},
            nickname2: {required: function () {
                return ($("#email2").val().length > 0 || $("#password2").val().length > 0 ? true : false)
            }, isNickname: '',notEqualTo: ["#nickname1",  "#nickname3", "#nickname4", "#nickname5", "#nickname6", "#nickname7"]},
            email3: {required: function () {
                return ($("#password3").val().length > 0 || $("#nickname3").val().length > 0 ? true : false)
            }, email: true, notEqualTo: ["#email1", "#email2", "#email4", "#email5", "#email6", "#email7"]},
            password3: {required: function () {
                return ($("#email3").val().length > 0 || $("#nickname3").val().length > 0 ? true : false)
            }},
            nickname3: {required: function () {
                return ($("#email3").val().length > 0 || $("#password3").val().length > 0 ? true : false)
            }, isNickname: '',notEqualTo: ["#nickname1", "#nickname2",  "#nickname4", "#nickname5", "#nickname6", "#nickname7"]},
            email4: {required: function () {
                return ($("#password4").val().length > 0 || $("#nickname4").val().length > 0 ? true : false)
            }, email: true, notEqualTo: ["#email1", "#email2", "#email3",  "#email5", "#email6", "#email7"]},
            password4: {required: function () {
                return ($("#email4").val().length > 0 || $("#nickname4").val().length > 0 ? true : false)
            }},
            nickname4: {required: function () {
                return ($("#email4").val().length > 0 || $("#password4").val().length > 0 ? true : false)
            }, isNickname: '',notEqualTo: ["#nickname1", "#nickname2", "#nickname3",  "#nickname5", "#nickname6", "#nickname7"]},
            email5: {required: function () {
                return ($("#password5").val().length > 0 || $("#nickname5").val().length > 0 ? true : false)
            }, email: true, notEqualTo: ["#email1", "#email2", "#email3", "#email4", "#email6", "#email7"]},
            password5: {required: function () {
                return ($("#email5").val().length > 0 || $("#nickname5").val().length > 0 ? true : false)
            }},
            nickname5: {required: function () {
                return ($("#email5").val().length > 0 || $("#password5").val().length > 0 ? true : false)
            }, isNickname: '',notEqualTo: ["#nickname1", "#nickname2", "#nickname3", "#nickname4",  "#nickname6", "#nickname7"]},
            email6: {required: function () {
                return ($("#password6").val().length > 0 || $("#nickname6").val().length > 0 ? true : false)
            }, email: true, notEqualTo: ["#email1", "#email2", "#email3", "#email4", "#email5", "#email7"]},
            password6: {required: function () {
                return ($("#email6").val().length > 0 || $("#nickname6").val().length > 0 ? true : false)
            }},
            nickname6: {required: function () {
                return ($("#email6").val().length > 0 || $("#password6").val().length > 0 ? true : false)
            }, isNickname: '',notEqualTo: ["#nickname1", "#nickname2", "#nickname3", "#nickname4", "#nickname5", "#nickname7"]},
            email7: {required: function () {
                return ($("#password7").val().length > 0 || $("#nickname7").val().length > 0 ? true : false)
            }, email: true, notEqualTo: ["#email1", "#email2", "#email3", "#email4", "#email5", "#email6"]},
            password7: {required: function () {
                return ($("#email7").val().length > 0 || $("#nickname7").val().length > 0 ? true : false)
            }},
            nickname7: {required: function () {
                return ($("#email7").val().length > 0 || $("#password7").val().length > 0 ? true : false)
            }, isNickname: '',notEqualTo: ["#nickname1", "#nickname2", "#nickname3", "#nickname4", "#nickname5", "#nickname6"]}
        },
        messages: {
            /* elements:{required:"请输入完整的注册信息"}*/
            family: {required: ""},
            email1: {required: "", email: "email",notEqualTo: "一个邮箱只能注册一个账号"},
            password1: {required: ""},
            nickname1: {required: "",notEqualTo: "昵称有重复"},
            email2: {required: "", email: "email",notEqualTo: "一个邮箱只能注册一个账号"},
            password2: {required: ""},
            nickname2: {required: "",notEqualTo: "昵称有重复"},
            email3: {required: "", email: "email",notEqualTo: "一个邮箱只能注册一个账号"},
            password3: {required: ""},
            nickname3: {required: "",notEqualTo: "昵称有重复"},
            email4: {required: "", email: "email",notEqualTo: "一个邮箱只能注册一个账号"},
            password4: {required: ""},
            nickname4: {required: "",notEqualTo: "昵称有重复"},
            email5: {required: "", email: "email",notEqualTo: "一个邮箱只能注册一个账号"},
            password5: {required: ""},
            nickname5: {required: "",notEqualTo: "昵称有重复"},
            email6: {required: "", email: "email",notEqualTo: "一个邮箱只能注册一个账号"},
            password6: {required: ""},
            nickname6: {required: "",notEqualTo: "昵称有重复"},
            email7: {required: "", email: "email",notEqualTo: "一个邮箱只能注册一个账号"},
            password7: {required: ""},
            nickname7: {required: "",notEqualTo: "昵称有重复"}
        },
        submitHandler: function () {
            var member_input=$("input[data-id='email']"),member_quantity= 0,password_input=$("input[data-id='password']");
            $.each(member_input,function(i,item){
                if($(item).val()){
                    member_quantity++;
                }
            });
            $.each(password_input,function(_i,_item){
                if($(_item).val()){
                   $(_item).val($.md5($(_item).val()))
                }
            });
            $("#quantity").val(member_quantity) ;
            var  param = $("#sing").serialize();
//            var wBox = $("body").wBox({title: "Test Title Name",
//                html: "<div class='demo'>点击弹出设置名字的wBox</div>"});
//
//                wBox.showBox();
            $.ajax({
                url: "login/register.php",
                type: "post",
                data: param,
                success:function(data){
                    data=eval("("+data+")");
                    if(data.message=="success"){
                        window.location.href=fm.config.buildPath+"account/index.html";
                    }
                }
            })
        }
    });


    //   todo 删除成员按钮，暂时不实现此删除功能
    /* $("#sing").delegate(".closeWrap","mouseenter",function(){$(this).find(".close").css("display","block")});
     $("#sing").delegate(".closeWrap","mouseleave",function(){$(this).find(".close").css("display","none")});
     $(".close").click(function(){
     $("this").parents(".memberWrap").remove();
     })*/
})

function showSingTip(Obj) {
    $(".sTip").hide().css({"bottom": "-50px", "left": "200px"});
    $(Obj).nextAll().css("display", "none");
    $(Obj).nextAll(".singTip") && $(Obj).nextAll(".singTip").css("display", "inline-block");
}

function showErrorTip(Obj) {
    $("#singUpBtn").addClass("dis");
    $(Obj).nextAll().css("display", "none");
    $(Obj).nextAll(".errorTip") && $(Obj).nextAll(".errorTip").css("display", "inline-block");
}
function showRepetitionTip(Obj) {
    $(Obj).nextAll().css("display", "none");               register
    $(Obj).nextAll(".repetitionTip") && $(Obj).nextAll(".repetitionTip").css("display", "inline-block");
}
function showPassTip(Obj) {
    $(Obj).nextAll().css("display", "none");
    $(Obj).nextAll(".passTip") && $(Obj).nextAll(".passTip").css("display", "inline-block");
    $("#singUpBtn").removeClass("dis");
}
function checkSame(array) {
    for (i = array.length; i > 0; i--) {
        for (j = array.length; j > 0; j--) {
            if (array[i] === array[i - 1]) {

            }
        }
    }
}


function inputBlur(Obj) {
    var sValue = $(Obj).val();
    if (sValue.trim() === "") {
        if ($(Obj).parents(".memberWrap").length) {
            var siblingsP = $(Obj).parent().siblings(), emptyMember = true, that = Obj;
            $.each(siblingsP, function (i, item) {
                if ($.trim($(item).find("input").val())) {
                    emptyMember = false;
                }
            })
            if (!emptyMember) {
                showErrorTip(that);
                return;
            } else {
                $(that).nextAll().hide();
                return;
            }
        } else {
            showErrorTip(Obj);
        }

    } else {
        var sName = $(Obj).attr("name"), matchWord = "";
        if (sName.search(/family/i) >= 0) {
            matchWord = "family";
        }
        if (sName.search(/email/i) >= 0) {
            matchWord = "email";
        }
        if (sName.search(/nickname/i) >= 0) {
            matchWord = "nickname";
        }
        switch (matchWord) {
            case "family" :
                var familyName = /^[\w\u4E00-\u9FA5\uF900-\uFA2D]{2,24}$/;
                var _this = Obj;
                if (familyName.test($(_this).val())) {
                    $.ajax({
                        url: "login/checkUser.php?method=checkFamily",
                        type: "post",
                        data: {"name": $(_this).val()},
                        success: function (data) {
                            data = eval("(" + data + ")");
                            if (data.data > 0) {
                                showRepetitionTip(_this);
                                return false;
                            }
                            showPassTip(_this);
                            return false;
                        }
                    });
                } else {
                    showErrorTip(_this);
                }
                break;
            case "email" :
                var isEmail = /^\w+([-\.]\w+)*@\w+([\.-]\w+)*\.\w{2,4}$/;
                var _this = Obj;
                if (isEmail.test($(_this).val())) {
                    var emailValList = $("input[data-id='email']"), emailArray = [];
                    $.each(emailValList, function (i, item) {
                        $(item).val() && emailArray.push($(item).val());
                    })
                    $.ajax({
                        url: "login/checkUser.php?method=checkEmail",
                        type: "post",
                        data: {"name": $(_this).val()},
                        success: function (data) {
                            if (data.data > 0) {
                                showRepetitionTip(_this);
                                return;
                            }
                            showPassTip(_this);
                            return;
                        }
                    })
                } else {
                    showErrorTip(_this);
                }
                break;
            case "nickname" :
                var nickname = /^[\w\u4E00-\u9FA5\uF900-\uFA2D]{2,24}$/;
                var _this = Obj;
                if (nickname.test($(_this).val())) {
                    $.ajax({
                        url: "login/checkUser.php?method=checkNickname",
                        type: "post",
                        data: {"name": $(_this).val()},
                        success: function (data) {
                            if (data.data > 0) {
                                showRepetitionTip(_this);
                                return;
                            }
                            showPassTip(_this);
                            return;
                        }
                    })

                } else {
                    showErrorTip(_this);
                }
                break;
        }
    }
}